package com.qh.book.api;

import java.util.HashMap;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.qh.book.VO.ResultVO;
import com.qh.book.converter.OrderForm2OrderDTO;
import com.qh.book.dto.OrderDTO;
import com.qh.book.enums.ResultEnum;
import com.qh.book.form.OrderForm;
import com.qh.book.service.OrderMasterService;
import com.qh.book.utils.ResultVOUtil;

@CrossOrigin
@RestController
@RequestMapping("/api/order")
public class OrderApi {
	
	@Autowired
	private OrderMasterService orderMasterService;


    //创建订单
    @PostMapping("/create")
    public ResultVO<Object> create(@Valid OrderForm orderForm,BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
        	return ResultVOUtil.returnError(ResultEnum.PARM_FAIL,bindingResult.getFieldError().getDefaultMessage());
        }

        OrderDTO orderDTO = OrderForm2OrderDTO.convert(orderForm);
        if (CollectionUtils.isEmpty(orderDTO.getOrderDetailList())) {
        	return ResultVOUtil.returnError(ResultEnum.CAR_ISEMPTY);
        }

        String OrderId = orderMasterService.create(orderDTO);

        return null;
    }
    
}
